package com.hx.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author wonghiu
 * @Date 2021/6/6 10:46
 * @Version 1.0.1
 */
@Configuration
public class MyMQConfig {
    /***
     * 一旦创建好，重复执行不会覆盖
     * @return
     */
    @Bean
    public Jackson2JsonMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,false);
    }
    @Bean
    public Queue orderMiaoShaOrderQueue(){
        return new Queue("order.miaosha.order.queue",true,false,false);
    }
    @Bean
    public Binding orderMiaoShaOrerQueueBingding(){
        return new Binding("order.miaosha.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.miaosha.order",
                null);
    }

    // 自动关单，延时队列
    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        // 信死了交给哪个exchange
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        // 信死了用哪个routing-key
        arguments.put("x-dead-letter-routing-key","order.release.order");
        // 过期时间
        arguments.put("x-message-ttl",60000);

        Queue queue = new Queue("order.delay.queue",true,false,false,arguments);
        return queue;
    }

    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue",true,false,false);
        return queue;
    }

    @Bean
    public Binding orderCreateOrderBingding(){
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }
    @Bean
    public Binding orderReleaseOrderBingding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    // 自动解锁库存
    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        // 信死了交给哪个exchange
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        // 信死了用哪个routing-key
        arguments.put("x-dead-letter-routing-key","order.release.stock");
        // 过期时间
        arguments.put("x-message-ttl",120000);

        Queue queue = new Queue("stock.delay.queue",true,false,false,arguments);
        return queue;
    }

    @Bean
    public Queue stockReleaseOrderQueue(){
        Queue queue = new Queue("order.release.stock.queue",true,false,false);
        return queue;
    }

    @Bean
    public Binding stockDelayOrderBingding(){
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.stock",
                null);
    }
    @Bean
    public Binding stockReleaseOrderBingding(){
        return new Binding("order.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.stock",
                null);
    }

    // 回滚消息队列
    @Bean
    public Queue callbackOrderQueue(){
        Queue queue = new Queue("order.callback.queue",true,false,false);
        return queue;
    }

    @Bean
    public Binding callbackOrderBingding(){
        return new Binding("order.callback.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.callback",
                null);
    }

}
